package com.xuxin.baby.model;

import com.blankj.utilcode.util.StringUtils;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.contract.ChildSmellyRecordContract;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.network.RetrofitApi;
import com.xuxin.baby.network.myObserver;
import com.xuxin.baby.presenter.ChildSmellyRecordPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xu_xin
 * Created on 2020/10/22 0022
 * 记臭臭
 */
public class ChildSmellyRecordModel implements ChildSmellyRecordContract.Model {
    @Override
    public void record(ChildSmellyRecordPresenter presenter,
                       String tenantId,
                       String token,
                       String eventDate,
                       String childId,
                       String eventTitle,
                       String eventContent,
                       String shitColor,
                       String shitCharacter,
                       String ShitQty,
                       String shitFreq,
                       String pic1) {
        try {
            JSONObject object = new JSONObject();
            object.put("eventDate", eventDate);
            object.put("childId", childId);
            object.put("eventTitle", eventTitle);
            object.put("eventContent", eventContent);
            object.put("shitColor", shitColor);
            object.put("shitCharacter", shitCharacter);
            object.put("ShitQty", ShitQty);
            object.put("shitFreq", shitFreq);
            if (!StringUtils.isEmpty(pic1)) {
                object.put("pic1", pic1);
            }
            RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
            Retrofit.getInstance().createReq(RetrofitApi.class)
                    .recordChildSmelly(tenantId, token,"1", requestBody)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new myObserver<BaseBean>() {
                        @Override
                        public void onNextX(@NonNull BaseBean baseBean) {
                            if (baseBean.isSuccess()) {
                                presenter.onSuccess(baseBean);
                            } else {
                                presenter.onError(baseBean);
                            }
                        }
                        @Override
                        public void onError(BaseBean baseBean) {
                            presenter.onError(baseBean);
                        }
                        @Override
                        public void onError(@NonNull Throwable e) {
                            presenter.onError(null);
                        }

                        @Override
                        public void onComplete() {
                            presenter.onComplete();
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}